package com.audi;

import java.util.ArrayList;
import java.util.List;

import lib.relay.DevListInfo;
import lib.relay.RegistRelayFunc.SignUpStatus;
import lib.relay.ThreadCtrlBase.ThreadRunListener;
import lib.relay.onAuthListener;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.DrawerLayout.DrawerListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationSet;
import android.view.animation.BounceInterpolator;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.audi.*;
import com.audi.activity.HomeBoxSettings;
import com.audi.activity.HomeControlActivity;
import com.audi.activity.HomeMemberActivity;
import com.audi.activity.PersonalPageActivity;
import com.audi.activity.SmartTriggerActivity;
import com.audi.adapter.MenuLeftAdapter;
import com.audi.box.objects.LocalData;
import com.audi.box.server.handler.HandlerFactory;
import com.audi.dto.MenuLeftItem;
import com.audi.message.Connector;

/*import com.asus.audi.box.server.MessageHandler;
 import com.asus.audi.box.server.handler.HandlerFactory;*/

public abstract class BaseActivity extends Activity implements onAuthListener,
		ThreadRunListener {
	protected DrawerLayout mDrawerLayout;
	protected LinearLayout mDrawerList;
	protected MenuLeftAdapter mMenuLeftAdapter;
	protected ArrayList<MenuLeftItem> mListMenuItem;
	private ActionBarDrawerToggle mDrawerToggle;
	private FrameLayout frame;
	protected ViewGroup mContentView;
	private float lastTranslate = 0.0f;
	private float lastTranslateHeight = 0.0f;
	private float lastSlideOffset = 1.0f;
	public int tempId = 0;
	public ImageView imvPersonalPage, homeMember;
	public TextView txtHomeControl, txtHomeAction, txtHomeBoxSetting,
			txtHistory, textView2;

	public abstract void onRefreshData();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.base_activity);

		if (Connector.SEND_MODE == Connector.iTH_Offline) {
			HandlerFactory.create(this);
		}

		mContentView = (ViewGroup) findViewById(R.id.frm_base_container);

		initMenuData();

		mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
		mDrawerList = (LinearLayout) findViewById(R.id.dl_left_drawer);
		frame = (FrameLayout) findViewById(R.id.frm_base_container);
		textView2= (TextView) findViewById(R.id.textView2);
		textView2.setText(LocalData.usename);
		mDrawerLayout.setScrimColor(Color.TRANSPARENT);
		mMenuLeftAdapter = new MenuLeftAdapter(this, mListMenuItem);
		mDrawerLayout.setDrawerShadow(R.color.transparent, GravityCompat.START);
		
		mDrawerLayout.setDrawerListener(new DrawerListener() {

			@Override
			public void onDrawerStateChanged(int arg0) {
			}

			@Override
			public void onDrawerSlide(View arg0, float arg1) {
			}

			@Override
			public void onDrawerOpened(View arg0) {

			}

			@Override
			public void onDrawerClosed(View arg0) {
				// initActionBar(R.layout.action_bar_menu);
				// initActionBar(tempId);

			}
		});
		mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
				R.drawable.ic_drawer, R.string.a_box, R.string.a_box) {
			public void onDrawerSlide(View drawerView, float slideOffset) {
				float moveFactor = (mDrawerList.getWidth() * slideOffset);
				float moveFactorHeight = (mDrawerList.getHeight() * slideOffset) * 0.12f;
				TranslateAnimation anim = new TranslateAnimation(lastTranslate,
						moveFactor, lastTranslateHeight, moveFactorHeight);
				anim.setDuration(0);
				anim.setFillAfter(true);

				AnimationSet animSet = new AnimationSet(true);
				animSet.setFillAfter(true);
				animSet.setDuration(0);
				animSet.setInterpolator(new BounceInterpolator());
				TranslateAnimation translate = new TranslateAnimation(
						lastTranslate, moveFactor, lastTranslateHeight,
						moveFactorHeight);
				animSet.addAnimation(translate);
				float chageFloat = 1.0f - slideOffset * 0.2f;
				ScaleAnimation scale = new ScaleAnimation(lastSlideOffset,
						chageFloat, lastSlideOffset, chageFloat,
						ScaleAnimation.RELATIVE_TO_PARENT, 1f,
						ScaleAnimation.RELATIVE_TO_PARENT, 0f);
				animSet.setFillBefore(true);
				animSet.addAnimation(scale);
				frame.startAnimation(animSet);
				// Log.d("tu tin", moveFactor + "-" + moveFactorHeight + "-"
				// + mDrawerList.getHeight());
				lastTranslate = moveFactor;
				lastSlideOffset = chageFloat;
				lastTranslateHeight = moveFactorHeight;
				// }
			}

			@Override
			public void onDrawerOpened(View drawerView) {
				// initActionBar(R.layout.action_bar_menu);
			}

			@Override
			public void onDrawerClosed(View drawerView) {
				// initActionBar(tempId);
				// initActionBar(tempId);
			}
		};
		mDrawerLayout.setBackgroundColor(getResources().getColor(
				R.color.color_bar));
		mDrawerLayout.setDrawerListener(mDrawerToggle);
		initViewBaseActivity();
	}

	private void initMenuData() {
		String[] menuLeftTitles = getResources().getStringArray(
				R.array.menu_left_array_title);
		String[] menuLeftNotic = getResources().getStringArray(
				R.array.menu_left_array_notic);
		TypedArray menuLeftIcons = getResources().obtainTypedArray(
				R.array.menu_left_array_icon);
		String[] menuLeftBottomLine = getResources().getStringArray(
				R.array.menu_left_array_bottom_line);
		TypedArray menuLeftBGColor = getResources().obtainTypedArray(
				R.array.menu_left_array_background_color);
		mListMenuItem = new ArrayList<MenuLeftItem>();
		for (int i = 0; i < menuLeftTitles.length; i++) {
			mListMenuItem
					.add(new MenuLeftItem(menuLeftTitles[i], menuLeftIcons
							.getResourceId(i, -1), menuLeftNotic[i],
							menuLeftBottomLine[i], menuLeftBGColor
									.getResourceId(i, -1)));
		}
		menuLeftIcons.recycle();
		menuLeftBGColor.recycle();
	}

	private void initViewBaseActivity() {
		imvPersonalPage = (ImageView) findViewById(R.id.imv_personal_page);
		imvPersonalPage.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent itentPersonalPage = new Intent(BaseActivity.this,
						PersonalPageActivity.class);
				itentPersonalPage.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				itentPersonalPage
						.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
				startActivity(itentPersonalPage);
//				finish();
			}
		});
		homeMember = (ImageView) findViewById(R.id.img_menu_home_member);
		homeMember.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent itentHomeMember = new Intent(BaseActivity.this,
						HomeMemberActivity.class);
				itentHomeMember.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				itentHomeMember.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
				startActivity(itentHomeMember);
			}
		});

		txtHomeControl = (TextView) findViewById(R.id.txt_menu_home_control);
		txtHomeControl.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent itentHomeControl = new Intent(BaseActivity.this,
						HomeControlActivity.class);
				itentHomeControl.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				itentHomeControl
						.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
				startActivity(itentHomeControl);
			}
		});

		txtHomeAction = (TextView) findViewById(R.id.txt_menu_home_action);
		txtHomeAction.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent itentHomeAction = new Intent(BaseActivity.this,
						SmartTriggerActivity.class);
				itentHomeAction.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				itentHomeAction.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
				startActivity(itentHomeAction);
			}
		});

		txtHomeBoxSetting = (TextView) findViewById(R.id.txt_box_setting);
		txtHomeBoxSetting.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent itentHomeBoxSetting = new Intent(BaseActivity.this,
						HomeBoxSettings.class);
				itentHomeBoxSetting.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				itentHomeBoxSetting
						.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
				startActivity(itentHomeBoxSetting);
			}
		});
		txtHistory = (TextView) findViewById(R.id.txt_history);
		txtHistory.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				/*Intent itentHistory = new Intent(BaseActivity.this,
						HistoryActivity.class);*/
				/*itentHistory.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				itentHistory.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
				startActivity(itentHistory);*/
			}
		});
	}

	@Override
	public void setContentView(View view, LayoutParams params) {
		if (params == null) {
			params = new LayoutParams(LayoutParams.MATCH_PARENT,
					LayoutParams.MATCH_PARENT);
		}
		if (view != null) {
			view.setLayoutParams(params);
			if (getContentView() != null) {
				getContentView().addView(view);
			} else {
				super.setContentView(view);
			}
		}
	}

	public ViewGroup getContentView() {
		return mContentView;
	}

	@Override
	public void setContentView(int layoutResID) {
		setContentView(getLayoutInflater().inflate(layoutResID, null));
	}

	@Override
	public void setContentView(View view) {
		setContentView(view, null);
	}

	protected void initActionBar(int actionBarView) {
		ViewGroup actionBarLayout = (ViewGroup) getLayoutInflater().inflate(
				actionBarView, null);
		ActionBar actionBar = getActionBar();
		actionBar.setDisplayShowHomeEnabled(false);
		actionBar.setDisplayShowTitleEnabled(false);
		actionBar.setDisplayShowCustomEnabled(true);
		actionBar.setCustomView(actionBarLayout, new ActionBar.LayoutParams(
				ViewGroup.LayoutParams.MATCH_PARENT,
				ViewGroup.LayoutParams.MATCH_PARENT));

	}

	public void setDisableDrawer() {
		mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
		// mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
	}

	@Override
	public void GetAuthFinished(boolean bValue) {

	}

	@Override
	public void ListProfileFinished(List<DevListInfo> relay_list) {

	}

	@Override
	public void LogoutFinished(boolean bValue) {

	}

	@Override
	public void CBInit(boolean bValue) {

	}

	@Override
	public void CBMakeCall(boolean bValue) {

	}

	@Override
	public void CBHangUp(boolean bValue) {

	}

	@Override
	public void CBDeInit(boolean bValue) {

	}

	@Override
	public void CBClientSocket(String CBString) {

	}

	@Override
	public void ThreadRun(int iID) {

	}

	@Override
	public void GetSignUp(SignUpStatus mSignUpStatus) {

	}

}
